package com.gitee.helioz.openai.api.endpoint.images;

import com.gitee.helioz.openai.api.OpenAiService;
import com.gitee.helioz.openai.api.dto.images.ImageEditRequest;
import com.gitee.helioz.openai.api.dto.images.ImageGenerationRequest;
import com.gitee.helioz.openai.api.dto.images.ImageGenerationResponse;
import com.gitee.helioz.openai.api.dto.images.ImageVariationRequest;
import com.gitee.helioz.openai.common.constants.OpenAiApiUrl;
import com.gitee.helioz.openai.common.util.BeanUtil;
import com.gitee.helioz.openai.common.util.http.HttpFilePart;
import com.gitee.helioz.openai.common.util.json.GsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 〈图片服务〉
 *
 * @author <a href="https://gitee.com/helioz">Morning Starr</a>
 */
@Slf4j
@RequiredArgsConstructor
public class ImageServiceImpl implements ImageService {
    private final OpenAiService openAiService;

    @Override
    public ImageGenerationResponse generations(ImageGenerationRequest imageRequest) {
        String requestJson = GsonUtil.toJson(imageRequest);
        String responseStr = openAiService.post(OpenAiApiUrl.Images.CREATE_IMAGE.getUrl(openAiService.getApiHost()), requestJson);
        return GsonUtil.fromJson(responseStr, ImageGenerationResponse.class);
    }

    @Override
    public ImageGenerationResponse edits(ImageEditRequest imageRequest) {
        String requestJson = GsonUtil.toJson(imageRequest);
        log.debug("编辑图片，请求={}", requestJson);
        Map<String, Object> formMap = BeanUtil.beanToMap(imageRequest, "image", "mask");
        List<HttpFilePart> fileParts = new ArrayList<>(3);
        HttpFilePart image = imageRequest.getImage();
        if (image != null) {
            image.setPartName("image");
            fileParts.add(image);
        }
        HttpFilePart mask = imageRequest.getMask();
        if (mask != null) {
            mask.setPartName("mask");
            fileParts.add(mask);
        }
        String responseStr = openAiService.postFormData(OpenAiApiUrl.Images.CREATE_IMAGE_EDIT.getUrl(openAiService.getApiHost()), formMap, fileParts);
        return GsonUtil.fromJson(responseStr, ImageGenerationResponse.class);
    }

    @Override
    public ImageGenerationResponse variations(ImageVariationRequest imageRequest) {
        String requestJson = GsonUtil.toJson(imageRequest);
        log.debug("图片变体，请求={}", requestJson);
        Map<String, Object> formMap = BeanUtil.beanToMap(imageRequest, "image");
        List<HttpFilePart> fileParts = new ArrayList<>(3);
        HttpFilePart image = imageRequest.getImage();
        if (image != null) {
            image.setPartName("image");
            fileParts.add(image);
        }
        String responseStr = openAiService.postFormData(OpenAiApiUrl.Images.CREATE_IMAGE_VARIATION.getUrl(openAiService.getApiHost()), formMap, fileParts);
        return GsonUtil.fromJson(responseStr, ImageGenerationResponse.class);
    }

}
